package com.maxatime.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.maxatime.dao.ActionTypeMapper;
import com.maxatime.dao.LogInfoMapper;
import com.maxatime.dao.UserInfoMapper;
import com.maxatime.pojo.entity.ActionType;
import com.maxatime.pojo.entity.LogInfo;
import com.maxatime.pojo.entity.UserInfo;
import com.maxatime.service.Ssbkaoshi7;
import com.maxatime.util.SerialNumberUtil;

@Service
public class SsbkaoshiImpl7 implements Ssbkaoshi7{
	
	@Autowired
	UserInfoMapper userInfoMapper;
	
	@Autowired
	LogInfoMapper logInfoMapper;
	
	@Autowired
	ActionTypeMapper actionTypeMapper;

	@Override
	public int deleteByPrimaryKey(Integer userId) {
		// TODO Auto-generated method stub
		userInfoMapper.deleteByPrimaryKey(userId);
		logInfoMapper.deleteByPrimaryKey(userId);
		actionTypeMapper.deleteByPrimaryKey(userId);
		return 0;
	}

	@Override
	public int insert(UserInfo record) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int insertSelective(UserInfo record) {
		// TODO Auto-generated method stub
		UserInfo userInfo  = new UserInfo();
		LogInfo logInfo = new LogInfo();
		ActionType actionType = new ActionType();
		Integer userId = SerialNumberUtil.getRandomNum();
		Integer actionId = SerialNumberUtil.getRandomNum();
		BeanUtils.copyProperties(record, userInfo);
		Date  d = new Date();
		userInfo.setRegTime(d);
		userInfo.setLoginTime(d);
		logInfo.setCreateTime(d);
		userInfo.setUserId(userId);
		logInfo.setUserId(userId);
		actionType.setUserId(userId);
		logInfo.setActionId(actionId);
		actionType.setActionId(actionId);
		userInfoMapper.insertSelective(userInfo);
		logInfoMapper.insertSelective(logInfo);
		actionTypeMapper.insertSelective(actionType);
		return 0;
	}

	@Override
	public UserInfo selectByPrimaryKey(Integer userId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int updateByPrimaryKeySelective(UserInfo record) {
		// TODO Auto-generated method stub
		UserInfo userInfo  = new UserInfo();
		LogInfo logInfo = new LogInfo();
		ActionType actionType = new ActionType();
		BeanUtils.copyProperties(record, userInfo);
		Date  d = new Date();
		userInfo.setRegTime(d);
		userInfo.setLoginTime(d);
		logInfo.setCreateTime(d);
		userInfoMapper.updateByPrimaryKeySelective(userInfo);
		logInfoMapper.updateByPrimaryKeySelective(logInfo);
//		actionTypeMapper.updateByPrimaryKeySelective(actionType);
		return 0;
	}

	@Override
	public int updateByPrimaryKey(UserInfo record) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Map<String, Object> map(UserInfo userInfo) {
		// TODO Auto-generated method stub
		PageHelper.startPage(userInfo.getPageNum(), userInfo.getPageSize());
		List<UserInfo> list = userInfoMapper.list(userInfo);
		PageInfo<UserInfo> pageinfo = new PageInfo<UserInfo>(list);
		Map<String, Object> m = new HashMap<>();
		m.put("list", list);
		m.put("pageInfo", pageinfo);
		return m;
	}

	@Override
	public UserInfo find(int userId) {
		// TODO Auto-generated method stub
		return userInfoMapper.find(userId);
	}

}
